﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ahi.Entity;
using MongoDB.Bson;
using Ahi.Controller;

namespace Ahi.Raporlama
{
    public partial class RaporSecimForm : DevExpress.XtraEditors.XtraForm
    {
        private RaporSecimForm()
        {
            InitializeComponent();
        }

        public RaporEntity SeciliRapor { get; private set; }

        private ObjectId VarsayilanRaporId;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            repositoryItemLookUpKayitGosterimSekli.SetEnumList<Ahi.Entity.Enums.KayitGosterimSekli>();

            var dataList = MongoAdaptor.Instance.GetData<RaporEntity>().OrderBy(x => x.Isim).ToList();
            dataList.Add(new RaporEntity
            {
                Isim = "[Formatsız Düz Liste]",
                Aciklama = "Ekranda gördüğünüz listeyi doğrudan yazdırır",
            });

            bindingSource1.DataSource = dataList;
            gridControl1.ForceInitialize();

            bindingSource1.MoveFirst();
            while (bindingSource1.Current is RaporEntity && (bindingSource1.Current as RaporEntity).Id != VarsayilanRaporId)
                bindingSource1.MoveNext();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            Sec();
        }

        private void Sec()
        {
            if (bindingSource1.Current == null) return;

            SeciliRapor = bindingSource1.Current as RaporEntity;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        internal static DialogResult ShowRaporSecim(ref RaporEntity rapor, ObjectId varsayilanId, string message = "Yazdırmak için kullanacağınız rapor formatını seçiniz:")
        {
            RaporSecimForm frm = new RaporSecimForm();
            frm.labelMessage.Text = message;
            frm.VarsayilanRaporId = varsayilanId;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                rapor = frm.SeciliRapor;
                return DialogResult.OK;
            }

            return DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            Sec();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Sec();
        }
    }
}
